import { computed, reactive } from "vue"
export function useRemindAudio(params = {}){
	const state = reactive({
		// 音频3次提醒
		remindNumber: 0,
		maxRemindNumber: 3,
		spaceTime: 15000,
		timer: null
	})
	
	// 提醒音频
	const remindAudioContext = wx.createInnerAudioContext({
	  useWebAudioImplement: false // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播放频繁的音频建议开启此选项，开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长，因此对于长音频建议关闭此选项
	})
	remindAudioContext.src = 'https://assets.mixkit.co/active_storage/sfx/933/933-preview.mp3'
	
	
	// 播放
	const play = () => {
		state.timer = setInterval(() => {
			if(state.remindNumber >= state.maxRemindNumber) {
				remindAudioContext.play()
				stopRemidnAudio()
				// 结束回调
				params.end && params.end()
				return
			}
			remindAudioContext.play()
			// 累加次数
			state.remindNumber++;
		}, state.spaceTime)
	}
	
	const stopRemidnAudio = () => {
		clearInterval(state.timer)
		state.timer = null
		state.remindNumber = 0
	}
	
	const playRemidnAudio = () => {
		remindAudioContext.play()
		// 累加次数
		state.remindNumber++;
		// 延时提醒
		play()
	}
	return {
		playRemidnAudio,
		stopRemidnAudio
	}
}